package com.zhou.common.lib.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     @author : loktar
 *     @e-mail : aakosp@gmail.com
 *     @time   : 2020-04-20
 *     @desc   :
 *     @version: 1.0
 * </pre>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonPage<T> {

    public static final CommonPage EMPTY = new CommonPage(0L, 0L, 1L, 10L, new ArrayList());

    public CommonPage(Long totalPage, Long totalCount, List<T> list) {
        this.totalPage = totalPage;
        this.totalCount = totalCount;
        this.list = list;
    }


    @ApiModelProperty(value = "总页码", example = "1")
    private Long totalPage;

    @ApiModelProperty(value = "总条数", example = "10")
    private Long totalCount;

    @ApiModelProperty(value = "页数", example = "1")
    private Long pageNo;

    @ApiModelProperty(value = "每页大小", example = "10")
    private Long pageSize;

    @ApiModelProperty(value = "数据")
    private List<T> list;

    public static <T> CommonPage<T> fromMybatisPage(IPage<T> pageRes) {
        CommonPage<T> result = new CommonPage();
        result.setTotalPage(pageRes.getPages());
        result.setTotalCount(pageRes.getTotal());
        result.setPageNo(pageRes.getCurrent());
        result.setPageSize(pageRes.getPages());
        result.setList(pageRes.getRecords());
        return result;
    }

}
